/*
 * File Name: MapUtil
 *------------------------------------------------------------------------------
 * Copyright 2012 NCR Corporation
 *------------------------------------------------------------------------------
 * All revision information is updated automatically from source code control
 * change records - please do not manually edit.
 *------------------------------------------------------------------------------
 *
 *  $LastChangedRevision:: 1569                                     $
 *  $LastChangedBy:: rb250096                                       $
 *  $LastChangedDate:: 2012-02-21 00:50:19 -0500 (Tue, 21 Feb 2012) $
 */

package com.ncr.nep.dashboardcore.utils.client;

import java.util.LinkedHashMap;
import java.util.Map;

/**
 * Utility class for manipulating maps.
 *
 * @author rb250096
 * @version $revision$
 */
public class MapUtil {

    /**
     * Generates a new map object from a set of key value pairs specified
     * as varargs.
     *
     * @param values List of key/value pairs to add to map.  There must be
     *               an even number of key/value pairs, and the types of keys
     *               and values must coincide with the types specified in
     *               T and U.
     * @param <T> The type of key.
     * @param <U> The type of value.
     *
     * @return Ordered map of key/value pairs specified in values.  The pairs
     * will be in the same order as they were specified in the function args.
     */
    @SuppressWarnings("unchecked")
    public static <T,U> Map<T, U> newMap(Object ...values) {
        if(values.length % 2 != 0) {
            throw new IllegalArgumentException("Must specify equal number" +
                    "of keys and values.");
        }

        LinkedHashMap<T, U> v = new LinkedHashMap<T, U>();
        for(int i = 0, l = values.length; i < l; i += 2) {
            v.put((T)values[i], (U)values[i + 1]);
        }
        
        return v;
    }
}
